package com.jifen.dynamic.service;

import com.jifen.common.CommonParam;
import com.jifen.dynamic.dao.DynamicInfoDao;
import com.jifen.dynamic.entity.DynamicInfo;
import com.jifen.dynamic.vo.DynamicVo;
import com.jifen.user.vo.UserJifenDetailVo;
import com.jifen.utils.SystemUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class DynamicService {

    @Autowired
    DynamicInfoDao dynamicInfoDao;

    /**
     * 动态列表
     * @param param
     * @return
     */
    public List<DynamicVo> getDynamic(CommonParam param, String url){
        param.setPageNo((param.getPageNo() - 1) * param.getPageSize());
        List<DynamicInfo> dynamicList = dynamicInfoDao.selectPage(param);
        List<DynamicVo> voList = new ArrayList<>();
        for (int i = 0; i < dynamicList.size(); i++) {
            DynamicVo vo = new DynamicVo();
            BeanUtils.copyProperties(dynamicList.get(i), vo);
            String content = SystemUtils.clearHtmlTag(vo.getContent());
            if(content.length()>100){
                vo.setContent(content.substring(0, 100)+"...");
            }
            vo.setDetailUrl(url+vo.getId());
            voList.add(vo);
        }
        return voList;
    }

    /**
     * 详情
     * @param id
     * @return
     */
    public DynamicVo detail(int id){
        DynamicInfo dynamicInfo = dynamicInfoDao.selectByPrimaryKey(id);
        DynamicVo vo = null;
        if(dynamicInfo!=null){
            vo = new DynamicVo();
            BeanUtils.copyProperties(dynamicInfo, vo);
        }
        return vo;
    }
}
